package com.itheima.itheima_health.handler;
import com.itheima.itheima_health.constant.MessageConstant;
import com.itheima.itheima_health.entity.Result;
import com.itheima.itheima_health.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

import static com.itheima.itheima_health.constant.MessageConstant.ADD_CHECKITEM_FAIL;

/**
 * 全局异常处理器，处理项目中抛出的业务异常
 */
@RestControllerAdvice //全局异常处理器注解
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 捕获业务异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public Result exceptionHandler(BaseException ex){
        log.error("异常信息：{}", ex.getMessage());
        return new Result(false,ex.getMessage());
    }

    /**
     * 捕获其他所有异常
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result handleOtherExceptions(Exception ex) {
        log.error("未知异常：{}", ex.getMessage(), ex);
        return new Result(false, "系统异常，请稍后重试或联系管理员");
    }
}
